package com.offcn.service;

import com.offcn.dao.*;
import com.offcn.entity.Course;
import com.offcn.entity.Course_User;
import com.offcn.entity.User;
import com.offcn.utils.PageUtils;

import javax.xml.ws.Service;
import java.util.List;

public class Course_UserServiceImpl implements Course_UserService {
    Course_UserDao course_userDao = new Course_UserDaoImpl();
    UserDao userDao = new UserDaoImpl();
    CourseDao courseDao = new CourseDaoImpl();

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param search
     * @return
     */
    @Override
    public PageUtils find(int currentPage, int pageSize, String search) {
//        查出总条数
     int total = course_userDao.find("SELECT co.* from  course_user co INNER JOIN user u on u.uid = co.uid INNER JOIN course c on co.cid =c.cid WHERE u.name LIKE ?","%"+search+"%").size();
//     分页查出数据存入集合
     List<Course_User> course_userList = course_userDao.find("SELECT co.* from  course_user co INNER JOIN user u on u.uid = co.uid INNER JOIN course c on co.cid =c.cid WHERE u.name LIKE ? LIMIT ? , ? ", "%" + search + "%", (currentPage - 1) * pageSize, pageSize);
//     遍历集合根据集合里面数据查出用户和课程里的所有信息
        for (Course_User course_user : course_userList) {
           User user = userDao.getUser(course_user.getUid());
            course_user.setUser(user);
           Course course = courseDao.getCourse(course_user.getCid());
           course_user.setCourse(course);
        }
//        存入实体类
     PageUtils pageUtils = new PageUtils(pageSize,currentPage,total,course_userList);

        return pageUtils ;
    }

    /**
     * 删除信息
     * @param ids
     * @return
     */
    @Override
    public boolean del(String[] ids) {
        try {
            for (String cid : ids) {
                course_userDao.del(cid);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 修改数据
     * @param cid
     * @param id
     * @return
     */
    @Override
    public boolean update(String cid, String id) {
        return course_userDao.update(cid,id)>0;
    }


}
